require(["common", "coAjax", "queryString", "hash_routie", "title"], function(jSouper, coAjax, QueryString, hash_routie, title) {
	// var get_goods_type = 2;
	// var current_page_num = 0;
	// var goods_pre_num = 12;
	// var pagenum;
	// $(function() {
	// 	getgoods();
	// });
	// $("a[name='prepage']").click(function() {
	// 	if (current_page_num > 0) {
	// 		current_page_num -= 1;
	// 		getgoods();
	// 	}
	// })
	// $("[name='nextpage']").click(function() {
	// 	if (current_page_num<pagenum) {
	// 		current_page_num += 1;
	// 		getgoods();
	// 	}
	// })
	hash_routie.on("goods_list", function() {
		var queryString = new QueryString(location.hash);
		// console.log(queryString);
		var get_goods_type = queryString.get("id");
		App.set("$Cahce.goods_list_type", get_goods_type);
		if (get_goods_type == 2) {
			// console.log("销量最高")
			App.set("$Cache.goods_info.type", "销量最高");
			title("销量最高");
			goods_list_url = appConfig.goods.quality_list;
		} else if (get_goods_type == 3) {
			// console.log("全部商品")
			App.set("$Cache.goods_info.type", "全部商品");
			title("全部商品");
			var goods_list_url = appConfig.goods.list;
		} else {
			// console.log("商品分类")
			App.set("$Cache.goods_info.type", "商品分类");
			title("商品分类");
			var goods_list_url = appConfig.goods.list;
		}

		//页号
		var current_page_num = ~~queryString.get("page");
		// console.log(current_page_num);
		//每页显示数量
		var goods_pre_num = ~~queryString.get("num") || appConfig.goods_pre_num || 12;
		App.set("config.goods_pre_num", goods_pre_num);
		App.set("$Cache.goods_pre_num", goods_pre_num);
		jSouper.ready(function(argument) {
			App.set("$Cache.current_page_num", current_page_num);
		});
		//获取指定页的商品
		coAjax.get(goods_list_url, {
			bus_id: appConfig.bus_id,
			num: goods_pre_num,
			page: current_page_num
		}, function(result) {
			// console.log("goods:", result.result);
			jSouper.ready(function() {
				App.set("goods", result.result);
			});
		});
		//获取所有商品数量
		coAjax.get(appConfig.goods.number, {
			bus_id: appConfig.bus_id,
		}, function(result) {
			jSouper.ready(function() {
				var number = ~~result.result
				App.set("$Cache.goods_number", number);
				var _goods_number_list = [];
				_goods_number_list.length = number;
				var _page_num = [];
				//向上取整获取页数
				// console.log(number, goods_pre_num, Math.ceil(number / goods_pre_num));
				_page_num.length = Math.ceil(number / goods_pre_num);
				App.set("$Cache.goods_number_list", _goods_number_list);
				App.set("$Cache.page_num", _page_num);
			});
		});
	});
	/*
	 * 添加到购物车
	 */
	function _get_cart_success(result) {
		var cart_detail = result.result;

		App.set("user.cart_detail", cart_detail);
		App.set("$Cache.cart.total_price", Model.Observer(function() {
			var cart_detail = App.get("user.cart_detail");
			var total_price = 0;
			cart_detail.forEach(function(cart) {
				cart.goods_info.forEach(function(goods_info) {
					total_price += goods_info.goods.price * goods_info.num;
				});
			});
			return total_price;
		}));
		alert("success", "添加成功")
	};

	function _get_cart_error(errorCode, xhr, errorMsg) {
		console.log("%c" + errorCode + ": " + errorMsg, "color:orange;font-size:14px; text-shadow: 0 1px 0 #ccc;");
		alert("error", errorMsg);
	};
	App.set("$Event.goods_list.add_to_cart", function(e, vm) {
		var cart_item = {
			goods_id: vm.get("_id"),
			num: 1
		};
		if (App.get("loginer")) {
			coAjax.post(appConfig.user.cart, cart_item, _get_cart_success, _get_cart_error);
		} else {
			var cache_cart = LS.get("$Cache.cart");
			try {
				cache_cart = JSON.parse(cache_cart);
			} catch (e) {
				console.error(e)
			}
			if (!(cache_cart instanceof Array)) {
				cache_cart = [];
			}
			cache_cart.push(cart_item);
			LS.set("$Cache.cart", JSON.stringify(cache_cart));
		}
	});
});